package com.higgins.eventtracking.server;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.higgins.eventtracking.server.dataModel.Asset;
import com.higgins.eventtracking.server.dataModel.HealthAlert;

@SuppressWarnings("serial")
public class CheckForHealthEventServlet extends HttpServlet	{
	private static final Logger log = Logger.getLogger(CheckForHealthEventServlet.class.getName());
	private DietTrackingDataService dietTrackingDataService = new DietTrackingDataService();

	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException	{
		log.info("CheckForHealthEventServlet is running!!");

		dietTrackingDataService.openPersistenceManager();
		checkHealthEvents();
		
		dietTrackingDataService.closePersistenceManager();
	}
	private void checkHealthEvents()	{
		log.info("CheckForHealthEventServlet is running checkHealthEvents() method!!");
		Boolean masterSwitch = dietTrackingDataService.retrieveMasterSwitch();
		List<HealthAlert> healthAlerts = dietTrackingDataService.retrieveHealthAlerts();
		log.info("There are " + healthAlerts.size() + " health alerts to check!!");
		for (HealthAlert healthAlert:healthAlerts)	{
			log.info("Checking " + healthAlert.getName() + " alert!!");
			if (dietTrackingDataService.sendHealthAlert(healthAlert) && masterSwitch)	{
				log.info("Sending email for " + healthAlert.getName() + " alert!!");
				sendEmail(healthAlert.getName());
			}
			if (healthAlert.getEventType().toLowerCase().equals("weather"))	{
				if (healthAlert.getOnOff())		{
					WeatherService weatherService = new WeatherService();
					String sunDetail = weatherService.checkForSun();
					if (sunDetail!=null)	{
						sendEmail("Sun Today!!!", sunDetail);
					}
						
				}
			}
		}
	}

	private void sendEmail(String message)	{
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = message;

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("pshiggins@gmail.com", "Patrick Higgins"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("pshiggins@gmail.com", "Patrick Higgins"));
            msg.setSubject(message);
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (Exception e) {

        }
	}
	private void sendEmail(String subject, String message)	{
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("pshiggins@gmail.com", "Patrick Higgins"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("pshiggins@gmail.com", "Patrick Higgins"));
            msg.setSubject(subject);
            msg.setText(message);
            Transport.send(msg);

        } catch (Exception e) {

        }
	}
}
